<?
session_start();
include_once("secure/connect.php");
include_once("secure/functions.php");
if (!$_SESSION['logged'])
	{
	print "SESSION FERMEE";
	die;
	}

/*print '<pre>';
print_r ($_POST);
print '</pre>';*/

$sql_look_fiche_loc="SELECT * FROM fiches_loc WHERE id='".$_GET['id_fiche_loc']."'";
$res_look_fiche_loc=mysql_query($sql_look_fiche_loc,$db);
$ligne_look_fiche_loc=mysql_fetch_assoc ($res_look_fiche_loc);

$sql_look_contact="SELECT * FROM contacts WHERE id='".$ligne_look_fiche_loc['client']."'";
$res_look_contact=mysql_query($sql_look_contact,$db);
$ligne_look_contact=mysql_fetch_assoc ($res_look_contact);

$sql_look_compte="SELECT * FROM cpt_comptes WHERE id_client='".$ligne_look_fiche_loc['client']."'";
$res_look_compte=mysql_query($sql_look_compte,$db);
$ligne_look_compte=mysql_fetch_assoc ($res_look_compte);

$color="#FFFFFF";


if ($_POST['Submit'] && $sql_look_fiche_loc['id'])
	{
	if ($_POST['visa']){
	$sql_look_journal="SELECT * FROM cpt_journal WHERE id='".$_GET['ecriture']."'";
	$res_look_journal=mysql_query($sql_look_journal,$db);
	$ligne_look_journal=mysql_fetch_assoc ($res_look_journal);
	$attributes="";
	$values="";
	$dumpExplain="";
	foreach ($ligne_look_journal as $key => $value) {
		$attributes=$attributes."`".$key."`, ";
		$values=$values."\'".$value."\', ";
		$dumpExplain=$dumpExplain.$key." : ".$value."\n";
		}
	$attributes=substr($attributes,0,-2);
	$values=substr($values,0,-2);

	
	$sql_insert_garbage="INSERT INTO cpt_journal_garbage (date, admin, dump, dumpExplain) VALUES (NOW(), ".$_POST['visa'].", '"."INSERT INTO cpt_journal (".$attributes.") VALUES (".$values.")', '".$dumpExplain."')";
	$sql_insert_encaisser="DELETE FROM cpt_journal WHERE id='".$_GET['ecriture']."'";
	if (mysql_query($sql_insert_garbage,$db)) {mysql_query($sql_insert_encaisser,$db);}
		}
		else {$color="#FF0000";}
	}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Document sans nom</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
.droite {
	text-align: right;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: x-small;
}
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: x-small;
}
.buton {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: x-small;
}
-->
</style>
</head>

<body b bgcolor="<? echo $color; ?>">
<form method="post" action="?id_fiche_loc=<? echo $_GET['id_fiche_loc'];?>&ecriture=<? echo $_GET['ecriture'];?>">
<?
if (!$_POST['visa'])
	{
	print "Effacer l'&eacute;criture du journal: <br>";
	 mk_list_table("visa",0,"SELECT id, user FROM admin WHERE active= '1' ORDER BY user", "id", "user", $db, TRUE); 
echo 'VISA obligatoire';
	}
else
	{ ?>
	Entrée effacée<br><br><a href="pop.form.encaisse_caution.php?id_fiche_loc=<? echo $_GET['id_fiche_loc'];?>">RETOUR</a><?
	}
?>
<table cellpadding="3">
<?
if (!$_POST['visa'])
{
$sql_journal="SELECT cptj.id, cptj.no_compte_debit, cptj.no_compte_credit, cptj.somme, a.user, cptj.date  FROM cpt_journal cptj, admin a WHERE cptj.id_admin=a.id AND cptj.id='".$_GET['ecriture']."'";
//$sql_journal="SELECT * FROM cpt_journal WHERE id_fiche_loc='".$_SESSION['id_fiche_loc']."' ORDER BY date";

$res_journal=mysql_query($sql_journal,$db);
while($ligne_journal=mysql_fetch_array($res_journal))
	{
	// recherche des libelleés des comptes
	$sql_credit="SELECT no, libelle_public FROM cpt_comptes WHERE no='".$ligne_journal['no_compte_credit']."'";
	$res_credit=mysql_query($sql_credit,$db);
	$ligne_credit=mysql_fetch_assoc($res_credit);
	
	$sql_debit="SELECT no, libelle_public FROM cpt_comptes WHERE no='".$ligne_journal['no_compte_debit']."'";
	$res_debit=mysql_query($sql_debit,$db);
	$ligne_debit=mysql_fetch_assoc($res_debit);
	
	if ($ligne_credit['no']==$ligne_look_fiche_loc['no'])
		{
		$solde=$solde-$ligne_journal['somme'];
		}
	else
		{
		$solde=$solde+$ligne_journal['somme'];
		}
	
	print "<tr><td>";
	if (substr($ligne_credit['no'],0,1)!=6)
		{
		print $ligne_journal['somme'].' payé par '.$ligne_credit['libelle_public'].' à '.$ligne_debit['libelle_public'].' le '.format_datetime($ligne_journal['date'],0);
		}
	else
		{
		print $ligne_journal['somme'].' facturé à '.$ligne_debit['libelle_public'].' pour '.$ligne_credit['libelle_public'].' le '.format_datetime($ligne_journal['date'],0);
		}
	print '<br><em>(id: '.$ligne_journal['id'].', '.$ligne_journal['user'].') '.$ligne_journal['commentaire'].'</em></td><td>';
	print "</td></tr>";
	}

}
?>
</table>
<?
if (!$_POST['visa']) {echo '<input name="Submit" type="submit" value="Effacer">';}
?>
</form>
</body>
</html>


